package com.whmskj.xjlhsc.qianYiData.job;

import com.whmskj.xjlhsc.qianYiData.service.DailyPowerSupplyCopyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDate;

@Slf4j
@Component
public class DailyPowerSupplyCopyJob {

    @Resource
    private DailyPowerSupplyCopyService dailyPowerSupplyCopyService;

    /**
     * 每3分钟滚动同步【当天】分班组电量到 itsADailyPowerSupplyCopy
     */
    @Scheduled(cron = "0 */2 * * * ?")
    public void syncToday() {
        try {
            LocalDate day = LocalDate.now();
            int affected = dailyPowerSupplyCopyService.syncDay(day);
            log.info("DailyPowerSupplyCopyJob[today]: synced {} affected {} rows", day, affected);
        } catch (Exception e) {
            log.error("DailyPowerSupplyCopyJob[today]: sync failed", e);
        }
    }

    /**
     * 每天00:10回填【昨天】分班组电量，确保每天都有数据
     */
    @Scheduled(cron = "0 10 0 * * ?")
    public void syncYesterday() {
        try {
            LocalDate day = LocalDate.now().minusDays(1);
            int affected = dailyPowerSupplyCopyService.syncDay(day);
            log.info("DailyPowerSupplyCopyJob[yesterday]: synced {} affected {} rows", day, affected);
        } catch (Exception e) {
            log.error("DailyPowerSupplyCopyJob[yesterday]: sync failed", e);
        }
    }
}

